import { Request, Response, NextFunction } from "express";
import { decodeToken } from "../helpers/token-decoder";

export const tokenVerifier = (request: Request, response: Response, next: NextFunction) => {
	try {
		const user = decodeToken(request.headers["authorization"] || "");
		if (!user) throw new Error("Token error");
		response.locals.user = user;
		next();
	} catch (err: any) {
		response.statusCode = 401;
		response.send(err.message);
	}
};
